### Figures APPENDIX


## produces figures displayed in the appendix V and VII
# Figures A V.1, A V.2, A V.3, A VI.1, A VI.2


# Session info
# R version 4.1.0 (2021-05-18)
# Platform: x86_64-apple-darwin17.0 (64-bit)
# Running under: macOS 13.1


# Load packages -----------------------------------------------------------
# if needed install packages first:
#install.packages(c("tidyverse", "gridExtra", "ggridges", "pwr"))

library(tidyverse)
library(gridExtra)
library(ggridges)
library(pwr)

# Load data ---------------------------------------------------------------

rm(list = ls())

# load report-level data
data_rep_level <- read_csv("data/data_rep_level.csv")

# load UN data
meta_pred_un <- read_csv("data/meta_pred_un.csv")

# define color palette
cbPalette <- c("#999999", "#E69F00", "#56B4E9", "#009E73", "#0072B2", "#000000")
cbPalette_2 <- c("#E69F00", "#56B4E9", "#009E73", "#0072B2", "#000000")

# Figures -----------------------------------------------------------------


########

### Appendix V: Power analysis

########

#### POWER ANALYSIS to determine whether 'Highly Unsatisfactory' rating underpowered
pwr.r.test(r = 0.76, sig.level = 0.05, n = 8, alternative = "two.sided")


##########

### Figure A V.1

##########


a <- data_rep_level %>%
  mutate(IEG_Outcome = factor(IEG_Outcome, levels = c("Highly Unsatisfactory", "Unsatisfactory","Moderately Unsatisfactory", "Moderately Satisfactory", "Satisfactory", "Highly Satisfactory"))) %>%
  group_by(IEG_Outcome) %>%
  count() %>%
  ggplot(aes(IEG_Outcome, n)) +
  geom_col(fill = "blue") +
  labs(x = " IEG Rating", y = "Number of reports") + 
  theme(text = element_text(size=5))
  


b <- meta_pred_un %>%
  ggplot(aes(sentiment)) +
  geom_histogram(fill = "blue") +
  labs(x = "Share of Positive Assessment", y = "Number of reports") +
  theme(text = element_text(size=5))

c <- grid.arrange(a, b, ncol = 2, nrow = 1)

ggsave("output/figureAV1.png", plot = c, scale = 1, 
       dpi = "retina", width = 27, height = 17, units = "cm")

##########

### Figure A V.2

##########


data_rep_level %>%
  mutate(IEG_Outcome = factor(IEG_Outcome, levels = c("Highly Unsatisfactory", "Unsatisfactory","Moderately Unsatisfactory", "Moderately Satisfactory", "Satisfactory", "Highly Satisfactory"))) %>% 
  ggplot(aes(x = sentiment, fill = IEG_Outcome)) +
  geom_histogram() +
  facet_wrap(~IEG_Outcome) +
  scale_fill_manual("IEG Outcome Rating",values=cbPalette) +
  xlab('Share of Positive Assessment') +
  ylab('Number of Observations')

# save plot 
ggsave("output/figureAV2.png", scale = 1, 
       dpi = "retina", width = 27, height = 17, units = "cm")



##########

### Figure A V.3

##########


med_sent_df <- data_rep_level %>%
  filter(IEG_Outcome != "Highly Unsatisfactory") %>%
  group_by(IEG_Outcome) %>%
  summarize(median=median(sentiment))

data_rep_level %>% 
  filter(IEG_Outcome != "Highly Unsatisfactory") %>%
  mutate(IEG_Outcome = factor(IEG_Outcome, levels = c("Unsatisfactory","Moderately Unsatisfactory", "Moderately Satisfactory", "Satisfactory", "Highly Satisfactory"))) %>% 
  ggplot(aes(x = sentiment, y = IEG_Outcome)) +
  geom_density(alpha = 0.3) +
  geom_density_ridges(aes(fill = IEG_Outcome), alpha = 0.3) +
  geom_vline(data = med_sent_df, aes(xintercept = median,
                                     color = IEG_Outcome), size=0.5) +
  scale_fill_manual(values=cbPalette_2) +
  scale_color_manual(values=cbPalette_2) +
  xlab('Share of Positive Assessment') +
  ylab("IEG Outcome Rating") +
  theme(axis.text.x = element_text(size = 9, hjust = ), 
        legend.position = 'none',
        axis.text.y = element_text(size = 8),
        axis.title.x = element_text(size=12),
        axis.title.y = element_text(size=12),
        plot.title = element_text(size = 16))

# save plot 
ggsave("output/figureAV3.png", scale = 1, 
       dpi = "retina", width = 27, height = 17, units = "cm")




########

### Figure A VI.1

########

# Residuals of model 3 in Table 1 of the main text 
model_iv_num_type_fe <- lm(sentiment ~ IEG_Outcome_num + 
                             factor(type) +
                             factor(country) +
                             factor(year),
                           data_rep_level)


# model_iv_num_type_fe in code
ggplot(model_iv_num_type_fe, aes(x = .fitted, y = .resid)) + geom_point() +
  labs(x='Predicted', 
       y='Residual') +
  geom_abline(intercept = 0, slope = 0, linetype = "dashed",
              color = "red") +
  geom_smooth(method = "lm", se = F)

# save plot 
ggsave("output/figureAVI1.png", scale = 1, 
       dpi = "retina", width = 27, height = 17, units = "cm")



########

### Figure A VI.2

########

### Create a training and test sets for checking model

# seed for reproducibility
set.seed(5000)

#create ID column for train/test sample
data_rep_level$id_tt <- 1:nrow(data_rep_level)

#use 70% of dataset as training set and 30% as test set 
train <- data_rep_level%>% dplyr::sample_frac(0.70)
test  <- dplyr::anti_join(data_rep_level, train, by = 'id_tt')
test <- test[test$country %in% train$country,]
test<- test[test$year %in% train$year,]

# create model for training data, same as model 3 in main text
model_train_test <- lm(sentiment ~ IEG_Outcome_num + 
                         factor(type) +
                         factor(country) +
                         factor(year), 
                       train)

# plotting predicted vs observed values for test data using model fitted on training data
ggplot(test, aes(x=predict(model_train_test, newdata = test), 
                 y = sentiment)) + 
  geom_point() +
  geom_abline(intercept = 0, slope = 1, color = "red") +
  xlim(0,1) +
  ylim(0,1) +
  labs(x='Predicted', 
       y='Observed') +
  theme(axis.text.x = element_text(size = 9), legend.position = 'none',
        axis.title.x = element_text(size=14, vjust = -0.7),
        axis.title.y = element_text(size=14, vjust = 1),
        plot.title = element_text(size = 16))

# save plot
ggsave("output/figureAVI2.png", scale = 1, 
       dpi = "retina", width = 27, height = 17, units = "cm")




